package com.guomao.demoa;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class JavaDemo3 {

    public static void main(String[] args) {


        Runnable r1 = () -> {
            for (int i = 0; i < 100; i++) {
                System.out.println(i);
            }
        };

        //如果大括号中的代码段只有一行 {}可以省略
        Runnable r2 = () -> System.out.println(1001);

        Runnable r3 = () -> {
            System.out.println(1002);
        };

        Consumer<Student> c1 = new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        };

        Consumer<Student> c2 = (Student s) -> {
            System.out.println(s);
        };

        //可推导可省略
        Consumer<Student> c3 = s -> System.out.println(s);


        Function<Student, Student> f1 = new Function<Student, Student>() {
            @Override
            public Student apply(Student s) {
                return s;
            }
        };

        Function<Student, Student> f2 = (Student s) -> {
            return s;
        };

        //省略数据类型 () return语句也可以省略
        Function<Student, Student> f3 = s -> s;


        Comparator<Student> cm1 = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return 0;
            }
        };


        Comparator<Student> cm2 = (Student s1, Student s2) -> {
            return 0;
        };

        Comparator<Student> cm3 = (s1, s2) -> 0;


        Predicate<Student> p1 = new Predicate<>() {
            @Override
            public boolean test(Student student) {
                return student == null;
            }
        };


        Predicate<Student> p2 = (Student s) -> {
            return s == null;
        };

        Predicate<Student> p3 = s -> s == null;


        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");

        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });


        list.forEach((String s) -> System.out.println(s));
        //方法引用
        list.forEach(System.out::println);


        MyInterA ma1 = new MyInterA() {
            @Override
            public String method(String a, int b, float c) {
                return a + b + c;
            }
        };


        MyInterA ma2 = (String a, int b, float c) -> a + b + c;

        //方法参数列表数据类型可以省略 多参数()不能省略
        MyInterA ma3 = (String a, int b, float c) -> a + b + c;
        MyInterA ma4 = (a, b, c) -> a + b + c;

        Comparator<Person> c = (ps1, ps2) -> {
            int x = ps1.getAge() - ps2.getAge();
            x = x == 0 ? ps1.getName().compareTo(ps2.getName()) : x;
            return x;
        };
    }

}
